<?php

namespace app\user\controller\mall\shop;

use app\common\controller\Userend;
use think\Db;
/**
 * 订单结算
 *
 * @icon fa fa-circle-o
 */
class Settlement extends Userend
{
    
    /**
     * Settlement模型对象
     * @var \app\common\model\mall\shop\Settlement
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\shop\Settlement;
        $this->view->assign("settlementtypeList", $this->model->getSettlementtypeList());
        $this->view->assign("settlementstatusList", $this->model->getSettlementstatusList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $shopId = cookie('shop_id');
            $total = $this->model
                ->where($where)
                ->where('shop_id',$shopId)
                ->order($sort, $order)
                ->count();
            $list = collection($this->model
                ->where($where)
                ->where('shop_id',$shopId)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select())->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 结算单详情
     */
    public function detail($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row) $this->error(__('No Results were found'));
        $userIds = $this->getDataLimitUserIds();
        if (is_array($userIds)) {
            if (!in_array($row[$this->dataLimitField], $userIds)) {
                $this->error(__('You have no permission'));
            }
        }
        $this->view->assign("row", $row);
        //获取结算单所包括的定单
        if ($this->request->isAjax()) {
            if(!$ids)$ids=$this->request->param("ids");
            $list = Db::name('mall_order_order')
                ->where('settlement_id',$ids)
                ->select();

            $result = array("total" => count($list), "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

}
